/*
 * Author: Reynier Ortiz
 * Date: Nov 9, 2013
 */
$(function () {
    var successMsgContainer = $("div[id*=successMessageContainer]");
    var errorMsgContainer = $("div[id*=errorMessageContainer]");
    var successMsg = $("span[id*=successMessage]");
    var errorMsg = $("span[id*=errorMessage]");
    var buttonEdit = $("button[id*=editUser]");
    var buttonDelete = $("button[id*=deleteUser]");
    buttonEdit.click(setSelectedUserValues);
    
    var $usersGrid = $("#usersGrid");
    
    if (successMsg.text()) successMsgContainer.show();
    if (errorMsg.text()) errorMsgContainer.show();
    
    loadUsers();
    
    $usersGrid.bind('rowselect', function (event) {
        var rowindex = event.args.rowindex;
        
        if (rowindex > -1) {
            buttonEdit.removeClass("disabled");
            buttonDelete.removeClass("disabled");
            buttonEdit.removeAttr("disabled");
            buttonDelete.removeAttr("disabled");
        }
        else {
            buttonEdit.class("disabled");
            buttonDelete.class("disabled");
            buttonEdit.attr("disabled", "disabled");
            buttonDelete.attr("disabled", "disabled");
        }
    });
    
    function setSelectedUserValues() {
        var rowindex = $usersGrid.jqxGrid('getselectedrowindex');
        var datarow = $usersGrid.jqxGrid('getrowdata', rowindex);
        $("input[id*=inputUserId]").val(datarow.UserId);
        $("input[id*=inputFirstName]").val(datarow.FirstName);
        $("input[id*=inputLastName]").val(datarow.LastName);
        $("input[id*=inputEmail]").val(datarow.Email);
        $("input[id*=inputUsername]").val(datarow.Username);
        $("input[id*=inputRoleId]").val(datarow.RoleId);
    }
    
    function loadUsers() {
        // prepare the data
        var source =
        {
            datatype: "json",
            datafields: [
                { name: 'UserId', type: 'string' },
                { name: 'FirstName', type: 'string' },
                { name: 'LastName', type: 'string' },
                { name: 'Email', type: 'string' },
                { name: 'Username', type: 'string' },
                { name: 'RoleId', type: 'int' },
                { name: 'RoleName', type: 'string' }
            ],
            data: { action: "loadUsers" },
            url: "./adminUsers"
        };

        var dataAdapter = new $.jqx.dataAdapter(source,
            {
                formatData: function (data) {
                    $.extend(data, {
                        featureClass: "P",
                        style: "full",
                        maxRows: 50
                    });

                    return data;
                }
            }
        );

        var dataAdapter = new $.jqx.dataAdapter(source, {
            downloadComplete: function (data, status, xhr) { },
            loadComplete:  function (data) {
                        return $.makeArray(data);
                    },
            loadError: function (xhr, status, error) { }
        });
        // initialize jqxGrid
        $usersGrid.jqxGrid(
        {
            width: 700,
            source: dataAdapter,
            theme: 'base',
            pageable: true,
            autoshowfiltericon: true,
            filterable: true,
            autoheight: true,
            sortable: true,
            altrows: true,
            enabletooltips: true,
            editable: false,
            columns: [
              { text: 'First Name', datafield: 'FirstName' },
              { text: 'Last Name', datafield: 'LastName' },
              { text: 'Email', datafield: 'Email' },
              { text: 'Username',  datafield: 'Username' },
              { text: 'Role Name', datafield: 'RoleName' }
            ]
        });
    }
});
